<?php
/* edit here change table to controller.  if this works should replace $table with $controller */
$table = $controller;

/*$this->load->view('templates/page_head');
$data = array( 
	'options' => array('fckeditor' => true)
);
$this->load->view('templates/admin/js_css_includes.php', $data);*/
$this->load->view(CMF_PREFIX . 'includes/page_start');
if( isset($scripts) && is_array($scripts)){
	foreach( $scripts as $script){
		?>
<script type="text/javascript" src="<?= $script ?>"></script>		
		<?
	}
}
?>
<title><?= (isset($page_title)?$page_title:"Add/Edit $table_name") ?></title>
</head>
<body>
<?php $this->load->view(CMF_PREFIX . (isset($menu_view)&&!empty($menu_view) ? $menu_view:'includes/menu')); ?>
<div class="admin_container">
	<h2><?= (isset($page_title)?$page_title:"Add/Edit $table_name") ?></h2>
	<?php 
$flash = $this->session->flashdata('cmf_message');
if( $flash ){
	$this->load->view(CMF_PREFIX . 'includes/flash_view', array('message' => $flash));
}
$errors = validation_errors();
if( $errors ){
	?>
	<div class="form_error_message">
		<?php
		echo $errors;
		?>
	</div>
	<?
}
echo form_open_multipart(CMF_URL . $table . '/save/' . $item->$id_field, array('id' => 'main_form', 'class' => $controller));

$containing_element = array( 'tag' => 'div', 'attributes' => array('class' => 'input_container'));
if( count($edit_fields)){
	foreach( $edit_fields as $key => $value){
		$containing_element['attributes']['id'] = $key . '_container';
		$value = $value['edit'];
		$hidden = isset($value['hidden'])&&$value['hidden']==true?true:false;
		if( is_array($value) ){
			$f = $value['func'];
			$params = is_array($value['params'])?$value['params']:array();
			$id = $key . '_help';
			$h = '';
			$hl = '';
			if(isset($value['help']) && !empty($value['help'])){
				$h = '<div class="input_help drawer" id="' . $id . '" style="display: none;">
						<div class="help_close ' . $id . '_action_close">x</div>
						<div class="help_header">Guidelines</div>
						<div class="help_text">' . $value['help'] . '</div>
					  </div>';
				$hl = '<a href="#" class="' . $id . '_action_toggle' . ' cmf_help_link">&nbsp;<img src="' . base_url() . 'assets/images/icons/52.png" alt="Help" /></a>';
			}
			$s = (isset($value['label']) && $value['label'])?'<label for="' . $key . '">' . $fields[$key]['name'] . $hl . '</label>':'';
			$k = isset($item->$key)?$item->$key:null;
			if( !$hidden ){
				echo $this->html->contain_in_element( $containing_element,
						$h . $s . call_user_func( $f, $key, $k, $params)
						);
			}else{
				echo $s . call_user_func( $f, $key, $k, $params);
			}
			
		}else if(is_string($value)){
			echo $value;
		}
	}
}

echo form_submit(array('name' => 'save', 'id' => 'save'), 'Save');
echo form_submit(array('name' => 'save_view', 'id' => 'save_view'), 'Save and View');
echo form_submit(array('name' => 'cancel', 'id' => 'cancel'), 'Cancel');
?>
</form>
</div>
<?
//$this->load->view('templates/page_foot');
$this->load->view(CMF_PREFIX . 'includes/page_end');
?>